$(document).ready(function () {

    $('#pop-wrapper select').select0();

    $('#pop-wrapper .x-4-move').dragging({
        move: 'both',
        randomPosition: false,
        handler: '.panel-heading'
    });

    /*保存*/
    $('#pop-wrapper .btn-4-save').off('click').on('click', function () {
        var id = $('#pop-wrapper input[name=id]').val();
        var url = 'menu/add.htm';
        if (id) {
            url = 'menu/modify.htm';
        }
        var pidOne = $('#pop-wrapper select.Lv1').val();
        var pidTwo = $('#pop-wrapper select.Lv2').val();
        if (pidTwo) {
            $('#pop-wrapper input[name=pid]').val(pidTwo);
        } else if (pidOne) {
            $('#pop-wrapper input[name=pid]').val(pidOne);
        } else {
            $('#pop-wrapper input[name=pid]').val('');
        }
        ajaxSubmitResponseJSON({
            url: url,
            method: 'POST',
            data: $('#pop-wrapper form').serialize(),
            success: function (data) {
                if (data.status == 0) {
                    closeLayer();
                    ajaxSubmitResponseHtm({
                        url: 'menu/query.htm'
                    });
                }
            }
        });
    });

    $('#pop-wrapper select.Lv1').off('change').on('change', function () {
        var id = $(this).val();
        if (!id) {
            $('#pop-wrapper select.Lv2').html('<option value="">-请选择-</option>');
            $('#pop-wrapper select.Lv2').select0();
            return;
        }
        ajaxSubmitResponseJSON({
            url: 'menu/dataGrid.htm',
            data: 'id=' + id,
            success: function (data) {
                var htm = '<option value="">-请选择-</option>';
                if (data.status == 0) {
                    var dataList = data.data;
                    for (var i = 0; i < dataList.length; i++) {
                        htm += '<option value="' + dataList[i].id + '">' + dataList[i].name + '</option>';
                    }
                }
                $('#pop-wrapper select.Lv2').html(htm);
                $('#pop-wrapper select.Lv2').select0();
            }
        });
    });
});